﻿namespace TodoApi.Foundation
{
    public class DataMapp
    {
        public static List<R> Mapping<R, T>(List<T> model)
        {
            List<R> resCom = new List<R>();
            for (var i = 0; i < model.Count; i++)
            {
                //创建该类的实例，通过非公有无参构造
                R result = Activator.CreateInstance<R>();
                foreach (var item in typeof(R).GetProperties())
                {
                    var pro = typeof(T).GetProperty(item.Name);
                    if (pro != null)
                    {
                        item.SetValue(result, pro.GetValue(model[i]).ToString());
                    }
                }
                resCom.Add(result);
            }
            return resCom;
        }
    }
}
